package com.xiangyun.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController

public class UploadController {
    @PostMapping("/upload")
    private String upload(MultipartFile updataload, HttpServletRequest httpServletRequest) throws IOException {
        //文件存储 把上传上来的文件存储下来
        System.out.println("上传资源存入对象");


        String realPath = httpServletRequest.getSession().getServletContext().getRealPath("/updataload/");
        File file = new File(realPath);
        if (!file.isDirectory()){
            file.mkdirs();
        }


        String oldName = updataload.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."),oldName.length());
        updataload.transferTo(new File(realPath,newName));
        String s = httpServletRequest.getScheme() + "://" + httpServletRequest.getServerName() + ":" + httpServletRequest.getServerPort()  + "/updataload/" + newName;

        return s;
    }

}
